package com.device.controller;

import com.device.domain.PowerBank;
import com.device.service.IPowerBankService;
import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.web.page.TableDataInfo;
import com.share.common.log.annotation.Log;
import com.share.common.log.enums.BusinessType;
import com.share.common.security.annotation.RequiresPermissions;
import com.share.common.security.utils.EntityAuditable.EntityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Tag(name = "充电宝接口管理")
@RestController
@RequestMapping("/powerBank")
public class PowerBankController extends BaseController {

    @Autowired
    IPowerBankService powerBankService;


    /**
     * 查询充电宝列表
     */
    @RequiresPermissions("device:powerBank:list")
    @Operation(summary = "查询充电宝列表")
    @GetMapping("/list")
    public TableDataInfo list(PowerBank powerBank) {
        startPage();
        List<PowerBank> list = powerBankService.selectPowerBankList(powerBank);
        return getDataTable(list);
    }


    /**
     * 新增充电宝
     */
    @Log(title = "充电宝管理", businessType = BusinessType.INSERT)
    @Operation(summary = "新增充电宝")
    @RequiresPermissions("device:powerBank:add")
    @PostMapping
    public AjaxResult add(@RequestBody PowerBank powerBank) {
        EntityUtils.setCreateInfo(powerBank);
        return toAjax(powerBankService.save(powerBank));
    }


    /**
     * 修改充电宝
     */
    @Log(title = "充电宝管理", businessType = BusinessType.UPDATE)
    @Operation(summary = "修改充电宝")
    @RequiresPermissions("device:powerBank:edit")
    @PutMapping
    public AjaxResult update(@RequestBody PowerBank powerBank) {
        EntityUtils.setUpdateInfo(powerBank);
        return toAjax(powerBankService.updateById(powerBank));
    }


    /**
     * 删除充电宝
     */
    @Log(title = "充电宝管理", businessType = BusinessType.UPDATE)
    @Operation(summary = "删除充电宝")
    @RequiresPermissions("device:powerBank:remove")
    @DeleteMapping("/{ids}")
    public AjaxResult deleteByIds(@PathVariable Long[] ids) {
        List<Long> list = Arrays.asList(ids);

        List<PowerBank> powerBankList = list.stream().map(id -> {
            PowerBank powerBank = new PowerBank();
            powerBank.setId(id);
            EntityUtils.setUpdateInfo(powerBank);
            return powerBank;
        }).toList();
        powerBankService.updateBatchById(powerBankList);

        return toAjax(powerBankService.removeBatchByIds(list));
    }


    /**
     * 获取充电宝详细信息
     */
    @RequiresPermissions("device:powerBank:query")
    @Operation(summary = "获取充电宝详细信息")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(powerBankService.getById(id));
    }


}
